import React, { Component } from 'react';
import './index.scss';
import { os } from 'amfe-env';
import { isWechat } from 'libs/tools';
import classNames from 'classnames'

export default class AppWakeup extends Component {
	state = {
		isIOS:true
	}

	componentDidMount (){
		this.isIOS();
		
		this.props.getInstance && this.props.getInstance({
			openApp: this.openApp,
			goDownload: this.goDownload
		});
	}

	isIOS = () => {
		let isIOS = !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
		this.setState({ isIOS });
	}


	openApp = () => {
		let { 
			path, 
			action, 
			cid, 
			type, 
			from_origin, 
			productId, 
			id 
		} = this.props;

		let downloadLink = os.isIOS ?
			'itms-apps://itunes.apple.com/us/app/%E5%BD%A9%E8%99%B9%E8%82%B2%E5%84%BF-%E4%B8%93%E4%B8%9A-%E8%AE%A9%E5%A6%88%E5%A6%88%E6%9B%B4%E8%BD%BB%E6%9D%BE/id1343226036?mt=8'
			: `https://a.app.qq.com/o/simple.jsp?pkgname=jzdoctor.xinqing.caihongyuer`;

		//  always show download tips,
		//  try to wakeup app on browser environment
		this.setState({ downloadLink, showDownloadTips: true });

		if (!isWechat()) {
			let wakeupLink, iosLink, androidLink;

			//  check from origin
			switch (from_origin) {
				case 'mainpage':
					iosLink = `caihongyueruserios://mainpage?cid=${cid}`;
					androidLink = `intent://jzapp.com/${path}?cid=${cid}#Intent;scheme=jzapp;action=${action};end`;
					break;
				default:
					//  set default wakeup links
					iosLink =`caihongyueruserios://wiki?cid=${cid}&type=${type}`;
					androidLink = `intent://jzapp.com/${path}?cid=${cid}#Intent;scheme=jzapp;action=${action};end`;
			}

			//  check action
			switch (action) {
				case 'open_product_list':
					iosLink = `caihongyueruserios://productDetail?productId=${productId}`;
					androidLink = `intent://jzapp.com/productdetail?productId=${productId}#Intent;scheme=jzapp;action=${action};end`;
					break;
				case 'open_ugc_text':
				case 'open_ugc_image':
				case 'open_ugc_video':
				case 'open_ugc_question':
					iosLink = `caihongyueruserios://${path}?id=${id}`;
					androidLink = `intent://jzapp.com/${path}?id=${id}#Intent;scheme=jzapp;action=${action};end`;
					break;
				case 'open_homepage':
						iosLink = `caihongyueruserios://`;
						androidLink = `intent://jzapp.com/#Intent;scheme=jzapp;action=Open_homepage;end`;
						break;
				case 'open_menses_calendar':
						iosLink = `caihongyueruserios://maternalhelper`;
						androidLink = `intent://jzapp.com/#Intent;scheme=jzapp;action=menses_calendar;end`;
						break;
				case 'open_pregnancy_assistance':
						iosLink = `caihongyueruserios://pregnancyhelper`;
						androidLink = `intent://jzapp.com/#Intent;scheme=jzapp;action=pregnancy_assistance;end`;
						break;
				default:
					//  already set default wakeup links in "check from origin" part
			}

			wakeupLink = os.isIOS ? iosLink : androidLink;
			location.href = wakeupLink;
		}
	}

	goDownload = () => {
		let downloadLink = os.isIOS ?
			'itms-apps://itunes.apple.com/us/app/%E5%BD%A9%E8%99%B9%E8%82%B2%E5%84%BF-%E4%B8%93%E4%B8%9A-%E8%AE%A9%E5%A6%88%E5%A6%88%E6%9B%B4%E8%BD%BB%E6%9D%BE/id1343226036?mt=8'
			: `https://a.app.qq.com/o/simple.jsp?pkgname=jzdoctor.xinqing.caihongyuer`;
		setTimeout(() => {
			location.href = downloadLink;
		}, 1000);
	}

	render() {
		let { showDownloadTips, downloadLink,isIOS } = this.state;
		let { top } = this.props;

		return (
			<React.Fragment>
				{
					showDownloadTips ?
						<div className="app-download-tips">
							<div className={`bd ${isIOS|| 'adr'}`} >
								<p className='fb'>彩虹育儿</p>
								<p className='fm'>一站式母婴健康医疗平台</p>
								<a className="download" href={downloadLink} onClick={this.goDownload}>点击下载</a>
							</div>
						</div>
						:
						<div className={classNames('app-invite', { top })} onClick={this.openApp}></div>
				}
			</React.Fragment>
		);
	}
}